﻿// Copyright (C) 2014  Patrick Maher

// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.

// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU General Public License for more details.

// You should have received a copy of the GNU General Public License
// along with this program.  If not, see <http://www.gnu.org/licenses/>.

using System.Collections.Generic;
using Paragon.Geometry;

namespace Paragon
{
    internal sealed class Viewport
    {
        public Vec3f CameraOrigin { get; set; }
        // Unit vector
        //public Vector3 Direction { get; set; }
        public Vec3f ScreenOrigin { get; set; }
        public int ScreenHeight { get; set; }
        public int ScreenWidth { get; set; }

        private List<Ray> _rayList = new List<Ray>();

        public Viewport(Vec3f cameraOrigin, Vec3f screenOrigin, int screenHeight, int screenWidth)
        {
            CameraOrigin = cameraOrigin;
            ScreenOrigin = screenOrigin;
            ScreenHeight = screenHeight;
            ScreenWidth = screenWidth;
        }

        //public int Render()
        //{
        //    //Vector3f d = ScreenOrigin - CameraOrigin;
        //    //Vector3 o = ScreenOrigin.Clamp();

        //    //Ray r = new Ray(CameraOrigin, d.Normalize());
        //    //int test = r.Stab();
        //    //Vector3f upperRight = new Vector3f(ScreenOrigin.X + ScreenWidth, ScreenOrigin.Y, ScreenOrigin.Z);
        //    //Vector3f lowerLeft = new Vector3f(ScreenOrigin.X, ScreenOrigin.Y + ScreenHeight, ScreenOrigin.Z);

        //    Ray r = null;
        //    Vector3f dir = null;
        //    Vector3f target = ScreenOrigin;

        //    for (int y = 0; y < ScreenHeight; y++)
        //    {
        //        for (int x = 0; x < ScreenWidth; x++)
        //        {
        //            target.X = ScreenOrigin.X + x;
        //            target.Y = ScreenOrigin.Y + y;
        //            dir = target - CameraOrigin;
        //            r = new Ray(CameraOrigin, dir.Normalize());
        //            int test = r.Stab();
        //            _rayList.Add(r);
        //        }
        //    }

        //    return 0;
        //}

        public Ray GetRay(int x, int y)
        {
            Vec3f dir = new Vec3f(ScreenOrigin.X, ScreenOrigin.Y, ScreenOrigin.Z);
            dir.X += x;
            dir.Y += y;
            dir -= CameraOrigin;
            return new Ray(CameraOrigin, dir.Normalize());
        }
    }
}